// more weapons code

function selChar() {
	// 
	var theSel = document.getElementById("select_character");
	var ix = theSel.selectedIndex;
	var theCharacter = theSel.options[ix].value;
	//var theCharacter = theSel.options[ix].text;
	//alert(theCharacter);
	if (theCharacter != "select a character") {
		document.getElementById('who').innerHTML = theSel.options[ix].text;
		theSel.options[ix].selected = true;
		datatable = characterWeapChart[theCharacter];
		weaponchart.draw(datatable, {"allowHtml" : true});
	}
}

function selectHandler(e) {
	//alert('There are ' + lightningchart.getSelection().length + ' items.');
	var selection = weaponchart.getSelection();

	var weap = "????";
	for ( var i = 0; i < selection.length; i++) {
		//alert('beep');
		var item = selection[i];
		//alert(item.row);
		//alert(item.column);
		var ld = lightningDataTable();
		//alert(ld.getFormattedValue(item.row,0));
		//alert(ld.getValue(item.row,0));
		// item.column is not null?
		/*
		if (item.row !== null && item.column !== null) {
		    alert("a");
		  weap = ld.getFormattedValue(item.row, item.column);
		} else if (item.row !== null) {
		    alert("b");
		  weap = ld.getFormattedValue(item.row, 0); //lightningData.getFormattedValue(item.row, 0);
		} else if (item.column !== null) {
		    alert("c");
		  weap = ld.getFormattedValue(0, item.column);
		} else {weap = "???";}
		 */
		weap = ld.getFormattedValue(item.row, 0);
	}

	var weap_ix = ld.getValue(item.row, 0);
	//alert(weapons_default[weap_ix]['base_exp']);
	/*
			"base_exp" : "300",
	"magic_incr" : "",
	"base" : "1",
	"star" : "26",
	"str_incr" : "",
	"base_str" : "55",
	"exp_incr" : "0",
	"base_magic" : "35"
	 */
	document.getElementById('weap').innerHTML = weap; //weapons_default[weap_ix]['base_exp'];
	document.getElementById('lv').innerHTML = document
			.getElementById('setCurrLv').value;
	document.getElementById('str').innerHTML = weap;
	document.getElementById('magic').innerHTML = weap;
	document.getElementById('target_lv').innerHTML = weap;
	document.getElementById('exp').innerHTML = weap;
	document.getElementById('needed_exp').innerHTML = weap;
	//lightningData.getValue(item.row, 0)
	//alert('The user selected ' + lightningchart.getSelection().length + ' items.');
	//alert('The user selected ' + weap);
}

// hacked originally from selectHandler
// for now
function weaponSelect() {
	var weap = "????";
	var selection = weaponchart.getSelection();
	var ld;
	var item;

	for ( var i = 0; i < selection.length; i++) {
		item = selection[i];
		ld = datatable;
		weap = ld.getFormattedValue(item.row, 0);
	}

	var weap_ix = ld.getValue(item.row, 0);

	var theData = weapons_default[weap_ix];
	var base = parseInt(theData['base'], 10);
	var max = parseInt(theData['star'], 10); // ??????

	//alert(base + "," + max + " from " + weap_ix);
	
	document.getElementById('setCurrLv').setAttribute('min', base);
	document.getElementById('setCurrLv').setAttribute('max', max - 1);
	document.getElementById('setCurrLv').value = base; //setAttribute('value', base);

	document.getElementById('setCurrLvMin').innerHTML = base;
	document.getElementById('setCurrLvMax').innerHTML = max - 1;

	var targetBase = base + 1;

	document.getElementById('setTargetLv').setAttribute('min', targetBase);
	document.getElementById('setTargetLv').setAttribute('max', max);
	document.getElementById('setTargetLv').value = max; //setAttribute('value', max);
	
	//alert(document.getElementById('setTargetLv').value+","+document.getElementById('setTargetLv').max);
	
	document.getElementById('setTargetLvMin').innerHTML = targetBase;
	//document.getElementById('setTargetLvMax').innerHTML = max;
	
	document.getElementById('setCurrLvValue').innerHTML = base;
	document.getElementById('setTargetLvValue').innerHTML = max;

	//

	document.getElementById('setCurrExp').value = "0";
	
	updateWeapon(weap_ix, weap);
}

function updateWeapon(whichWeapon, weap) {
	// compute
	//alert("calculating");
	//document.getElementById('cvw').innerHTML = "hitty stick";

	var theData = weapons_default[whichWeapon];

	var currentLevel = parseInt(document.getElementById('setCurrLv').value, 10); //theData['base'];
	//document.getElementById('setCurrLvValue').innerHTML = currentLevel;
	//prefs.set('weapon_current_level', currentLevel); //parseInt(prefs.getString('weapon_current_level'),10);
	var currentExp = parseInt(document.getElementById('setCurrExp').value, 10); //0; //parseInt(prefs.getString('weapon_current_exp'),10);

	var targetLevel = parseInt(document.getElementById('setTargetLv').value, 10); //"*"; //prefs.getString('weapon_target_level');

      //alert("all your bases");
      var baseExp = parseInt(theData['base_exp'], 10);
      var expIncr = parseInt(theData['exp_incr'], 10);
      var baseStr = parseInt(theData['base_str'], 10);
      var strIncr = parseInt(theData['str_incr'], 10);
      var baseMagic = parseInt(theData['base_magic'], 10);
      var magicIncr = parseInt(theData['magic_incr'], 10);




	//var levels = targetLevel - currentLevel; //theData['base'];

	//var currLevels = currentLevel; // - theData['base'];
      //alert("are ours");
	var currStr = baseStr + (strIncr * (currentLevel - 1));
	var currMagic = baseMagic + (magicIncr * (currentLevel - 1));
	var str = baseStr + (strIncr * (targetLevel - 1));
	var magic = baseMagic + (magicIncr * (targetLevel - 1));





	var exp = 0;
	var expn = 0;
     var currExp = 0;

	/*
      for (n = 1; n < currentLevel; n++) {
		expn = baseExp + expIncr * (n - 1);
		currExp = currExp + expn;
	}
      */
      //alert("calculating");
      // exp = currExp;
	for (n = currentLevel; n < targetLevel; n++) {
		expn = baseExp + expIncr * (n - 1);
		exp = exp + expn;
	}


	exp = exp - currentExp;

	//alert("we calculated");
	document.getElementById('weap').innerHTML = weap;
	document.getElementById('lv').innerHTML = currentLevel;
	document.getElementById('str').innerHTML = currStr;
	document.getElementById('magic').innerHTML = currMagic;
	document.getElementById('exp').innerHTML = currentExp;

	document.getElementById('target_lv').innerHTML = targetLevel;
	document.getElementById('target_str').innerHTML = str;
	document.getElementById('target_magic').innerHTML = magic;
	document.getElementById('needed_exp').innerHTML = exp;

	//document.getElementById('setCurrLvValue').innerHTML = currentLevel;
	//document.getElementById('setTargetLvValue').innerHTML = targetLevel;

}

// button when we change things
function updateCurrentForWeapon() {
	//
	//alert('aaa');
	
	//var weap = "????";
	var selection = weaponchart.getSelection();
	//var ld;
	var item = selection[0];

	/*
      for ( var i = 0; i < selection.length; i++) {
		item = selection[i];
		//ld = datatable;
		weap = ld.getFormattedValue(item.row, 0);
	}
      */

	//alert('bbb');
      var weap = datatable.getFormattedValue(item.row, 0); // always one selected currently
	var weap_ix = datatable.getValue(item.row, 0);

	//var theData = weapons_default[weap_ix];
	//var base = theData['base'];
	//var max = theData['star'];
	
	

	//document.getElementById('setCurrLvValue').innerHTML = document.getElementById('setCurrLv').value;
	//document.getElementById('setTargetLvValue').innerHTML = max;
	
	updateWeapon(weap_ix, weap);
	
	//alert('and back');
	//return false;
}


